package com.stylefeng.guns.rest.modular.departdata.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.stylefeng.guns.rest.common.persistence.dao.DepartClickMapper;
import com.stylefeng.guns.rest.common.persistence.dao.DepartDataMapper;
import com.stylefeng.guns.rest.common.persistence.dao.DepartShowMapper;
import com.stylefeng.guns.rest.common.persistence.dao.WithdrowMapper;
import com.stylefeng.guns.rest.common.persistence.model.DepartClick;
import com.stylefeng.guns.rest.common.persistence.model.DepartData;
import com.stylefeng.guns.rest.common.persistence.model.DepartShow;
import com.stylefeng.guns.rest.common.persistence.model.WithDrow;
import com.stylefeng.guns.rest.modular.departdata.controller.dto.DepartDataResponse;
import com.stylefeng.guns.rest.modular.departdata.controller.dto.DepartRequest;
import com.stylefeng.guns.rest.modular.departdata.controller.dto.DepartShowRequest;
import com.stylefeng.guns.rest.modular.departdata.controller.dto.WithdrowDetailRequest;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ASUS-PC on 2019/9/14.
 */
@Service
public class DepartShowService {
    private final Log logger = org.apache.commons.logging.LogFactory.getLog(this.getClass());
    @Autowired
    DepartShowMapper departShowMapper;
    @Autowired
    DepartClickMapper departClickMapper;
    @Autowired
    DepartDataMapper departDataMapper;
    @Autowired
    WithdrowMapper withdrowMapper;


    /**
     * 插入数据
     * @return
     */
    public int showDataInsert(DepartShowRequest request){
        //查询是否已存在
        List<DepartShow> list = departShowMapper.selectList(Condition.create().eq("user_id" , request.getUserId()).eq("article_id",request.getArticleId()));
        if(list!=null && list.size()>0){
            DepartShow departShow = list.get(0);
            departShow.setCreateTime(new Date());
            departShow.setNums(departShow.getNums()+1);
            return departShowMapper.update(departShow,Condition.create().eq("id",departShow.getId())) ==1?1:0;
        }else {
            DepartShow departShow = new DepartShow();
            departShow.setUserId(request.getUserId());
            departShow.setArticleId(request.getArticleId());
            departShow.setNums(1);
            departShow.setCreateTime(new Date());
            return departShowMapper.insert(departShow)==1 ?1:0;
        }
    }

    /**
     * 插入数据
     * @return
     */
    public int clickDataInsert(DepartShowRequest request){
        //查询是否已存在
        List<DepartClick> list = departClickMapper.selectList(Condition.create().eq("user_id" , request.getUserId()).eq("article_id",request.getArticleId()));
        if(list!=null && list.size()>0){
            DepartClick departShow = list.get(0);
            departShow.setCreateTime(new Date());
            departShow.setNums(departShow.getNums()+1);
            return departClickMapper.update(departShow,Condition.create().eq("id",departShow.getId())) ==1?1:0;
        }else {
            DepartClick departShow = new DepartClick();
            departShow.setUserId(request.getUserId());
            departShow.setArticleId(request.getArticleId());
            departShow.setNums(1);
            departShow.setCreateTime(new Date());
            return departClickMapper.insert(departShow)==1 ?1:0;
        }
    }

    /**
     * 更新点击或者展示总数(暂时废弃)
     */
    public void updateDepartData(int type,DepartShowRequest request){
        if(type ==0){//更新展示数
            List<DepartData> mList = departDataMapper.selectList(Condition.create().eq("user_id",request.getUserId()));
            if(mList!=null && mList.size()>0){
                DepartData departData = mList.get(0);
                departData.setShowNums(departData.getShowNums()+1);
                departData.setCreateTime(new Date());
                departDataMapper.update(departData,Condition.create().eq("user_id",request.getUserId()));
            }else {
                DepartData departData = new DepartData();
                departData.setCreateTime(new Date());
                departData.setShowNums(1);
                departData.setUserId(request.getUserId());
                departDataMapper.insert(departData);
            }
        }else {//更新点击数
            List<DepartData> mList = departDataMapper.selectList(Condition.create().eq("user_id",request.getUserId()));
            if(mList!=null && mList.size()>0){
                DepartData departData = mList.get(0);
                departData.setClickNums(departData.getClickNums()+1);
                departData.setCreateTime(new Date());
                departDataMapper.update(departData,Condition.create().eq("user_id",request.getUserId()));
            }else {
                DepartData departData = new DepartData();
                departData.setCreateTime(new Date());
                departData.setClickNums(1);
                departData.setUserId(request.getUserId());
                departDataMapper.insert(departData);
            }
        }
    }

    /**
     * 更新点击或者展示总数
     * @param type
     * @param request
     */
    public void updateDepartData2(int type,DepartShowRequest request){
        if(type==0){
            Map<String ,Object> datas = new HashedMap();
            datas.put("userId",request.getUserId());
            DepartData departData = departDataMapper.queryByUserid(datas);
            if(departData!=null){
                departData.setShowNums(departData.getShowNums()+1);
                departDataMapper.updateById(departData);
            }else {
                DepartData d = new DepartData();
                d.setShowNums(1);
                d.setCreateTime(new Date());
                d.setUserId(request.getUserId());
                departDataMapper.insert(d);
            }
        }else {
            Map<String ,Object> datas = new HashedMap();
            datas.put("userId",request.getUserId());
            DepartData departData = departDataMapper.queryByUserid(datas);
            if(departData!=null){
                departData.setClickNums(departData.getClickNums()+1);
                departDataMapper.updateById(departData);
            }else {
                DepartData d = new DepartData();
                d.setClickNums(1);
                d.setCreateTime(new Date());
                d.setUserId(request.getUserId());
                departDataMapper.insert(d);
            }
        }
    }

    //获取用户近三个月的收益详情
    public List<DepartDataResponse> getUserDepartDatas(DepartRequest request){
        Map<String,Object> data = new HashedMap();
        data.put("userId",request.getUserId());
        data.put("pageIndex" , (request.getPage()-1)*request.getSize());
        data.put("pageSize",request.getSize());
        return departDataMapper.getUserDepartDatas(data);
    }

    //获取用户未体现的所有收益
    public List<DepartData> getUserDepartMoney(DepartRequest request){
        return departDataMapper.selectList(Condition.create().eq("user_id",request.getUserId()).eq("state",0));
    }
    //提交用户的提现金额
    public int insertWthdrow(WithDrow withDrow){
        return withdrowMapper.insert(withDrow);
    }
    //获取用户提现明细
    public List<WithDrow> userWithdrowList(DepartRequest request){
        Map<String,Object> data = new HashedMap();
        data.put("userId",request.getUserId());
        data.put("pageIndex" , (request.getPage()-1)*request.getSize());
        data.put("pageSize",request.getSize());
        return withdrowMapper.userWithdrowList(data);
    }

    //获取提现详情
    public WithDrow getWithDrowDetail(WithdrowDetailRequest request){
        List<WithDrow> list = withdrowMapper.selectList(Condition.create().eq("id",request.getId()));
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }
    //查询用户是否有审核中的提现记录
    public boolean getWithdrowingList(int userId){
        Map<String,Object> data = new HashedMap();
        data.put("userId",userId);
        return withdrowMapper.userWithdrowingList(data).size()>0 ? true:false;
    }
}
